<script>
import { api } from '@/api';
import dayjs from 'dayjs';
import { List } from 'vant';

export default {
  components: {
    List
  },
  computed: {
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 0
    }
  },
  methods: {
    async getList() {
      const { data, count } = await api.news.list(this.page++)

      if (!Array.isArray(data) || !data.length) {
        return true
      }

      this.list.push(...data.map(item => ({
        ...item,
        update_time: dayjs(item.update_time).format('M月D日 HH:mm')
      })))

      return this.list.length >= count
    },
    view(id) {
      this.$router.push({ path: `/article/${id}` })
    },
    async handleLoad() {
      this.finished = await this.getList()
      this.loading = false
    }
  },
}
</script>

<template>
  <div class="news-view">
    <List v-model="loading" :finished="finished" finished-text="没有更多了" @load="handleLoad">
      <div class="news-list">
        <div v-for="item in list" :key="item.article_id" class="news-item" @click="view(item.article_id)">
          <img class="news-item-avatar" :src="item.file_url ? item.file_url : undefined" alt="">
          <div class="news-item-content">
            <div class="news-item-title">{{ item.article_title }}</div>
            <div class="news-item-date">{{ item.update_time }}</div>
          </div>
        </div>
      </div>
    </List>
  </div>
</template>

<style lang="less" scoped>
.news-view {
  height: 100%;

  .news-list {
    height: 100%;

    .news-item {
      display: flex;
      padding: 15px;
      gap: 20px;

      &:not(:last-child) {
        border-bottom: 1px solid #f0f0f0;
      }

      .news-item-avatar {
        width: 80px;
        height: 80px;
        border-radius: 4px;
        object-fit: cover;
      }

      .news-item-content {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        padding: 4px 0;
      }

      .news-item-title {
        // 两行缩略
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
      }

      .news-item-date {
        font-size: 12px;
        color: #999;
      }
    }
  }
}
</style>
